<template>
  <!-- 流水分析 -->
  <div class="app-container">
    <div class="app-wrap">
      <search-table ref="table" :table-loading="listLoading" :table-data="dataList" @query="getDataList(true)" @reset="refresh">
        <template v-slot:row>
          <el-form-item label="渠道:" label-width="50px">
            <el-select v-model="listQuery.agent_id" placeholder="请选择渠道" filterable >
              <el-option label="全部" value="" ></el-option>
              <el-option :label="'【'+item.value+'】'+item.label" :value="item.value" v-for="(item, index) in channelData" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item  label="日期时间:" label-width="80px">
            <el-date-picker is-range v-model="listQuery.dateTime" type="daterange" range-separator="至" start-placeholder="开始日期" 
            end-placeholder="结束日期" class="full-width-input time-box" :default-time="['00:00:00', '23:59:59']" :clearable="false"></el-date-picker>
          </el-form-item>
        </template>
        <template v-slot:actions>
          <div class="flexDetween">
            <el-button type="info" size="mini" icon="el-icon-refresh" @click="getDataList">刷新</el-button>
            <el-button type="primary" size="mini" icon="el-icon-download" :disabled="!dataList.length" @click="getExport">导出</el-button> 
          </div>
        </template>
        <template>
          <el-table-column prop="date" label="日期时间" align="center" width="150"/>
          <el-table-column prop="agent_id" label="渠道名" align="center" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.agent_id?'【'+scope.row.agent_id+'】'+$public.channelLabel(scope.row.agent_id):'' }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" prop="game_id" label="游戏类型" width="120">
            <template slot-scope="scope">
              <span >{{ scope.row.game_id?gameLabel(scope.row.game_id):'汇总' }}</span>
            </template>
          </el-table-column> -->
          <el-table-column prop="cash_mode" label="玩家类型" align="center" width="120">
            <template slot-scope="scope">
              <el-tag :type="scope.row.cash_mode?'':'success'">{{ scope.row.cash_mode?'正常玩家':'新手玩家'}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="send_reward" label="赠送筹码" align="center" />
          <el-table-column prop="recharge_reward" label="充值筹码" align="center" />
          <el-table-column prop="amount" label="剩余筹码" align="center" />
          <el-table-column prop="water_total" label="流水汇总" align="center" />
        </template>
        <template v-slot:footer>
          <pagination :total="currentTotal" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getDataList" />
        </template>
      </search-table>
    </div>
  </div>
</template>

<script>
import {
  throttle
} from '@/utils/index'
import {
  getFluvial,
  getFluvialExport
} from '@/api/statistics'
 import {
  GAME_TYPE
} from '@/utils/enum'
import SearchTable from '@/components/SearchTable'
export default {
  name: 'fluvialAnalysis',
  components: {
    SearchTable
  },
  data () {
    return {
      GAME_TYPE,
      channelData:this.$store.state.commonState.channelDataArray,
      listQuery: {
        page: 1,
        size: 50,
        agent_id:'',
        game_id:1,
        dateTime: [this.$public.getDateWeek('s',0)+' 00:00:00',this.$public.getDateWeek('e',0)+' 23:59:59'],
        start_time: '',
        end_time: '',
      },
      listLoading: true,
      dataList: [],
      currentTotal: 0,
    }
  },
  created () {
    // this.getDataList();
  },
  activated(){
    this.channelData=this.$store.state.commonState.channelDataArray
    this.getDataList();
  },
  methods: {
    // 流水分析
    async getDataList (query) {
      let that = this,params=that.listQuery;
      if(query && !query.page){
        params.page=1
        params.size=50
      }
      that.dataList = []
      that.checkedItem = null
      if (params.dateTime && params.dateTime[0] && params.dateTime[1]) {
        params.start_time = that.$public.standardDate(params.dateTime[0])
        params.end_time = that.$public.standardDate(params.dateTime[1])
      }else{
        params.start_time = ''
        params.end_time = ''
      }
      const response = await getFluvial(params)
      if (response.code == 1) {
        if(response.data.list){
          response.data.list.forEach(item=>{
            item.date=item.date.substr(0,10)
            item.send_reward=that.$public.mathNumberDivideWan(item.send_reward)
            item.recharge_reward=that.$public.mathNumberDivideWan(item.recharge_reward)
            item.amount=that.$public.mathNumberDivideWan(item.amount)
            item.water_total=that.$public.mathNumberDivideWan(item.water_total)
          })
          that.dataList = response.data.list
          that.currentTotal = response.data.total
        }
      }else{
        that.$message.error(response.msg)
      }
      that.listLoading = false
    },
    // 导出
    getExport(){
      let that = this,serveUrl=process.env.VUE_APP_BASE_API.substring(0,process.env.VUE_APP_BASE_API.length-6);
      getFluvialExport(that.listQuery).then(res => {
        if (res.code == 1) {
          window.open(serveUrl+res.data)
        }
      }).catch((error) => {
        that.$message.error(error.msg)
      })
    },
    // 游戏类型展示
    gameLabel(cellValue){
      if(cellValue){
        return (this.GAME_TYPE.find((item) => item.value === cellValue)?.label || "-");
      }else{
        return '--'
      }
    },
    // 刷新
    refresh: throttle(function () {
      this.listQuery = {
        page: 1,
        size: 50,
        agent_id:'',
        game_id:1,
        dateTime: [this.$public.getDateWeek('s',0)+' 00:00:00',this.$public.getDateWeek('e',0)+' 23:59:59'],
        start_time: '',
        end_time: '',
      }
      this.getDataList()
    }, 2000),
  }
}
</script>

<style lang="scss" scoped>

</style>
